package com.cyandpos.pad.cateringformat.choosemenu;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.CyFood;

import java.util.List;

public class FoodTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CyFood> list;
    private Context context;
    private int index=0;
    private OnItemClickListener onItemClickListener;
    public FoodTypeAdapter(List<CyFood> list, Context context){
        this.list=list;
        this.context=context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHoler(LayoutInflater.from(context).inflate(R.layout.item_food_type,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ((ViewHoler)holder).tv_type_name.setText(list.get(position).getName());
          if(index==position){
              ((ViewHoler)holder).tv_type_name.setBackgroundResource(R.drawable.food_type_bg_true);
          }else{
              ((ViewHoler)holder).tv_type_name.setBackgroundResource(R.drawable.food_type_bg_false);
          }
        ((ViewHoler)holder).tv_type_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list!=null?list.size():0;
    }

    class ViewHoler extends RecyclerView.ViewHolder{
        private TextView tv_type_name;
        public ViewHoler(View itemView) {
            super(itemView);
            tv_type_name=itemView.findViewById(R.id.tv_type_name);
        }
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
